package com.cat.greedyAlgorithm;

/**
*   @description https://leetcode.cn/problems/longest-balanced-substring-i/
*   @author 曲大人的喵
*   @create 2025/10/12 13:21
*   @since JDK17
*/

public class Solution79 {
    public int longestBalanced(String s) {
        char[] arr = s.toCharArray();
        int ans = 0, n = arr.length;
        for (int i = 0; i < n; i++) {
            int[] h = new int[26];
            int x = 0, y = 0;
            for (int j = i; j < n; j++) {
                int c = arr[i] - 'a';
                x += h[c]++ == 0 ? 1 : 0;
                y = Math.max(y, h[c]);
                if (x * y == j - i + 1) {
                    ans = Math.max(ans, x * y);
                }
            }
        }

        return ans;
    }
}
